<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 文章控制模型
 * @author wb_wenjie.wuwj <wb_wenjie.wuwj@alibaba-inc.com>
 * @copyright 2013-2-20
 */

class Article extends MY_Controller
{
	function __construct()
	{
		parent::__construct();
		
		// 后台登录验证
		$this->check_login();
		
		// 验证权限
		$this->verify_purview(PURVIEW_ARTICLE);
		
		$this->load->model('article_m');
		
		// 加载文章的语言包
		$this->load->language('article');
		
		$this->validate_data = array(
				array('field' => 'title', 'label' => 'lang:article_title', 'rules' => 'trim|required|max_length[50]|xss_clean'),
				array('field' => 'editor', 'label' => 'lang:article_editor', 'rules' => 'trim|required|max_length[20]'),
				array('field' => 'author', 'label' => 'lang:article_author', 'rules' => 'trim|required|max_length[20]'),
				array('field' => 'source', 'label' => 'lang:article_source', 'rules' => 'trim|required|max_length[15]'),
				array('field' => 'path', 'label' => 'lang:article_attachment', 'rules' => 'trim|max_length[200]'),
				array('field' => 'content', 'label' => 'lang:article_content', 'rules' => 'trim|required'),
				array('field' => 'keyword', 'label' => 'lang:com_keyword', 'rules' => 'trim|max_length[30]'),
				array('field' => 'classid', 'label' => 'lang:com_column_name', 'rules' => 'trim|required|is_natural_no_zero'),
				array('field' => 'releasename', 'label' => 'lang:com_releasename', 'rules' => 'trim|required|max_length[20]'),
				array('field' => 'remark', 'label' => 'lang:com_remark', 'rules' => 'trim|max_length[20]'),
				);
	}
	
	function index($page = '')
	{
		$this->manage($page);
	}
	
	/**
	 * 显示文章列表
	 * @param int $page 显示的页码
	 */
	function manage($page = '')
	{
		// 获取要显示的页数
		$page           = empty($page) ? 1 : abs(intval($page));
	
		// 组织分页信息
		$config['base_url']		= base_url('article/manage');
		$config['total_rows']	= $this->article_m->get_article_count();
		
		$page_data = $this->get_page($config);
		$data['page'] 	= $page_data['page'];
		$order 			= 'istop desc,isrecommend desc,id desc';
	
		// 查询文章
		$article      = $this->article_m->show_article($page_data['limit'], $order);
	
		// 文章信息列表数组
		$i = 1;
		foreach ($article as $key => $row)
		{
			$article[$key]['ordernum'] 	= ($page - 1) * $page_data['per_page'] + $i;
			$article[$key]['createtime']	= date(DATE_FORMAT,$row['createtime']);
			$article[$key]['updatetime']	= date(DATE_FORMAT,$row['updatetime']);
			$article[$key]['isactive'] 	= $row['isactive'] == 0 ? $this->lang->line('com_sign_inactive') : $this->lang->line('com_sign_active');
			$article[$key]['isverify'] 	= $row['isverify'] == 0 ? $this->lang->line('com_sign_not_verify') : $this->lang->line('com_sign_verify');
			$article[$key]['isrecommend'] = $row['isrecommend'] == 1 ? $this->lang->line('com_sign_recommend') : '';
			$article[$key]['istop'] 		= $row['istop'] == 1 ? $this->lang->line('com_sign_top') : '';
			$i++;
		}
	
		$data['header'] 		= $this->header;
		$data['title']			= $this->lang->line('article_title_list');
		$data['page_label']		= $this->lang->line('article_page_label');
		$data['nav_label'] 		= $this->lang->line('com_navigate_label');
		$data['article'] 	= $article;
		$this->load->view('article_list',$data);
	}
	
	/**
	 * ajax删除文章，返回json结果
	 */
	function delete_action()
	{
		// 对表单数据做判断
		$validate = $this->validation(array(array('field' => 'id[]', 'label' => 'lang:article_id', 'rules' => 'trim|required|is_natural|xss_clean')));
		if (TRUE !== $validate)
		{
			echo $this->array2json(TRUE, $validate);
			return ;
		}
	
		// 获取文章ID值，并放入数组
		$id = $this->input->post('id');
		if ($this->article_m->delete_article_by_id($id))
		{
			echo $this->array2json(FALSE, $this->lang->line('msg_operation_success'));
			return ;
		}
		else
		{
			echo $this->array2json(TRUE, $this->lang->line('msg_operation_failed'));
			return ;
		}
	}
	
	/**
	 * 显示文章添加页面
	 */
	function add()
	{
		$data['header'] 		= $this->header;
		$data['title']			= $this->lang->line('article_title_add');
		$data['page_label']		= $this->lang->line('article_page_label');
		$data['nav_label'] 		= $this->lang->line('com_navigate_label');
		$data['loginname'] 		= $this->loginname;
		$data['class'] 			= $this->get_column_as_array();
		$this->load->view('article_add', $data);
	}
	
	/**
	 * 添加文章
	 */
	function add_action()
	{
		$validate = $this->validation($this->validate_data);
		if ($validate !== TRUE)
		{
			$this->_show_message(base_url('article/add'), $validate, 'goback');
		}
	
		$article = $this->input->post();
		$content = array();
		$attachment = array();
		if ($article && isset($article['s_article_add']))
		{
			// 判断添加的类别是否是子类
			if (!$this->has_sub_column($article['classid']))
			{
				$this->_show_message(base_url('article/add'), $this->lang->line('error_choice_sub_column'), 'goback');
			}
				
			// 组建附件数组
			if (!empty($article['path']))
			{
				$attachment['title'] = $article['title'];
				$attachment['path'] = $article['path'];
				$attachment['type'] = 'article';
			}
	
			$article['updatetime'] = $article['createtime'] = time();
			$content['content'] = $article['content'];
			$content['keyword'] = $article['keyword'];
	
			// 清除数组中无用的数据，添加相关信息
			unset($article['s_article_add']);
			unset($article['content']);
			unset($article['keyword']);
			unset($article['path']);
	
			// 添加
			$result = $this->article_m->insert_article($article, $content, $attachment);
			if ($result)
			{
				$this->_show_message(base_url('article'), $this->lang->line('msg_operation_success'));
			}
			else
			{
				$this->_show_message(base_url('article/add'), $this->lang->line('msg_operation_failed'), 'goback');
			}
		}
		else
		{
			$this->_show_message(base_url('article'), $this->lang->line('error_invalid_post'));
		}
	}
	
	/**
	 * 显示编辑文章页面
	 * @param int $id 文章id
	 */
	function edit($id)
	{
		$article_id['article.id'] = $id = intval($id);
		if (empty($article_id))
		{
			$this->_show_message(base_url('article'), $this->lang->line('article_id_unable_empty'), 'goback');
		}
	
		// 查询信息
		$article = $this->article_m->get_article($article_id);
		if (empty($article))
		{
			$this->_show_message(base_url('article'), $this->lang->line('article_does_not_exist'), 'goback');
		}
		$article = $article[0];
		$article['path'] = '';
	
		// 查询相对应的附件
		// @TODO 只选出一个，可以选出多个
		$this->load->model('attachment_m');
		$attachment = $this->attachment_m->get_attachment(array('bindid' => $id, 'type' => 'article'));
		if (!empty($attachment))
		{
			$article['path'] = $attachment[0]['path'];
		}
		$data = array (
					
				'header' => $this->header,
				'article' => $article,
				'class' => $this->get_column_as_array(),
				'loginname'	=> $this->loginname,
				'title' => $this->lang->line('article_title_edit'),
				'page_label' => $this->lang->line('article_page_label'),
				'nav_label' => $this->lang->line('com_navigate_label'),
				'redirect' => $this->get_referer_url(),
		);
		$this->load->view('article_edit', $data);
	}
	
	/**
	 * 编辑文章
	 */
	function edit_action()
	{
		$this->load->model('attachment_m');
		$redirect = empty($_GET['redirect']) ? base_url('article') : $_GET['redirect'];
		
		$validate = $this->validation($this->validate_data);
		if ($validate !== TRUE)
		{
			$this->_show_message(base_url('article/manage'), $validate, 'goback');
		}
	
		$article = $this->input->post();
		$content = array();
		$attachment = array();
		if ($article && isset($article['s_article_edit']))
		{
			// 判断添加的类别是否是子类
			if (!$this->has_sub_column($article['classid']))
			{
				$this->_show_message(base_url('article/manage'), $this->lang->line('error_choice_sub_column'), 'goback');
			}
				
			// 文章id不能为空
			$id = $article['article_id'];
			if (empty($id))
			{
				$this->_show_message(base_url('article/manage'), $this->lang->line('article_id_unable_empty'), 'goback');
			}
				
			// 更新文章附件，不做旧附件的删除
			$attachment_count = $this->attachment_m->count_all_results(array('bindid' => $id, 'type' => 'article'));
			if ($attachment_count >= 1)
			{
				// 编辑后，附件不为空，直接更新，否则删除原来的附件及表记录
				if (!empty($article['path']))
				{
					$attachment['title'] = $article['title'];
					$attachment['path'] = $article['path'];
				}
				else
				{
					$attachment['bindid'] = 0;
				}
			}
			else
			{
				// 原来没有附件，编辑之后添加附件
				if (!empty($article['path']))
				{
					$attachment['bindid'] = $id;
					$attachment['title'] = $article['title'];
					$attachment['path'] = $article['path'];
					$attachment['type'] = 'article';
					$this->attachment_m->insert_attachment($attachment);
					$attachment = array();
				}
			}
				
			$article['updatetime'] = time();
			$content['content'] = $article['content'];
			$content['keyword'] = $article['keyword'];
				
			// 清除数组中无用的数据，添加相关信息
			unset($article['article_id']);
			unset($article['origin_classid']);
			unset($article['s_article_edit']);
			unset($article['content']);
			unset($article['keyword']);
			unset($article['path']);
				
			// 更新
			$result = $this->article_m->update_article(array('id' => $id), $article);
			$this->article_m->update_article_content(array('articleid' => $id), $content);
			if (!empty($attachment))
			{
				$result = $this->attachment_m->update_attachment(array('bindid' => $id, 'type' => 'article'), $attachment);
			}
				
			if ($result)
			{
				$this->_show_message($redirect, $this->lang->line('msg_operation_success'));
			}
			else
			{
				$this->_show_message(base_url('article'), $this->lang->line('msg_operation_failed'), 'goback');
			}
		}
		else
		{
			$this->_show_message(base_url('article'), $this->lang->line('error_invalid_post'));
		}
	}
	
	/**
	 * 显示文章状态设置页面
	 * @param string $id 文章id
	 */
	function article_state($id)
	{
		$id = (int)$id;
		$article = $this->article_m->get_article_state(array('id' => $id), '1');
		if (empty($article))
		{
			$this->_show_message(base_url('article'), $this->lang->line('article_does_not_exist'), 'goback');
		}
		$article = $article[0];
	
		$data = array (
					
				'header' => $this->header,
				'state' => $article,
				'title' => $this->lang->line('article_title_state_config'),
				'model' => 'article'
		);
		$this->load->view('state_config', $data);
	}
	
	/*
	 * @see MY_Controller::config_state()
	*/
	function config_state()
	{
		echo parent::config_state('article_m', 'article');
		return ;
	}
}